import GameConfig from "./GameConfig";
import MaoRandom from "./MaoRandom";

export default class Bullet extends Laya.Sprite{
    constructor(){
        super()
    }

    st = 0;
    speed = 200/1000;
    vx = 0;
    vy = 0;
    txt:Laya.Text = new Laya.Text;
    count = 0;

    public setdata(color:string,now:number){
        this.graphics.clear();
        this.graphics.drawCircle(0, 0, 15, color);
        this.x = GameConfig.width / 2;
        this.y = GameConfig.height / 2;

		var tx = (MaoRandom.seededRandom()) * 400 - 200;
        var ty = (MaoRandom.seededRandom()) * 400 - 200;
        this.speed = (200 + ( (MaoRandom.seededRandom()*800) ))/1000;

        var m = Math.atan2(ty,tx);
        this. vx = Math.cos(m) * this.speed;
        this. vy = Math.sin(m) * this.speed;
        this.addChild(this.txt);
        this.txt.autoSize = true;
        this.txt.fontSize = 10;
        this.txt.color = "#ffffff";

        this.st = now;
    }

    public start(){
        this.txt.text = this.count + "";
        this.txt.x = this.txt.width/-2; 
        this.txt.y = this.txt.height/-2;               
        Laya.stage.frameLoop(1,this,this.efe);
    }

    efe(){
        var ct = Date.now();
        var dt = ct - this.st;
        if(dt<0)dt = 0;
        var xx = GameConfig.width / 2 + this.vx*dt;
        var yy = GameConfig.height / 2 + this.vy*dt;

        if ( (xx< -50 || xx > GameConfig.width+50 ) || (yy < -50 || yy > GameConfig.height + 50) ){
            this.removeSelf();
            Laya.stage.clearTimer(this,this.efe);
            Laya.Pool.recover("Bullet",this);
            return;
        }

        this.x = xx;
        this.y = yy;
    }



    public static getBullet(){
        var b = Laya.Pool.getItemByClass("Bullet",Bullet) as Bullet;
        b.count++;
        return b;
    }
}